home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / chesssrc.zip / BOOK.C < prev    next >
Text File  |  1990-08-31  |  6KB  |  203 lines

  1. /*
  2.   C source for GNU CHESS
  3.  
  4.   Revision: 1990-09-30
  5.  
  6.   Modified by Daryl Baker for use in MS WINDOWS environment
  7.  
  8.   Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  9.   Copyright (c) 1988, 1989, 1990  John Stanback
  10.  
  11.   This file is part of CHESS.
  12.  
  13.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  14.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  15.   the consequences of using it or for whether it serves any particular
  16.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  17.   General Public License for full details.
  18.  
  19.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  20.   only under the conditions described in the CHESS General Public License.
  21.   A copy of this license is supposed to have been given to you along with
  22.   CHESS so you can know your rights and responsibilities.  It should be in a
  23.   file named COPYING.  Among other things, the copyright notice and this
  24.   notice must be preserved on all copies.
  25. */
  26.  
  27. #define NOATOM 
  28. #define NOCLIPBOARD
  29. #define NOCREATESTRUCT
  30. #define NOFONT
  31. #define NOREGION
  32. #define NOSOUND
  33. #define NOWH
  34. #define NOCOMM
  35. #define NOKANJI
  36.  
  37. #include <windows.h>
  38. #include <time.h>
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41.  
  42. #include "gnuchess.h"
  43. #include "defs.h"
  44. #include "chess.h"
  45.  
  46. static unsigned int book_used = 0;
  47. static char far * xBook;
  48. GLOBALHANDLE hBook = 0;
  49.  
  50. void FreeBook (void)
  51. {
  52.    GlobalUnlock ( hBook );
  53.    GlobalFree ( hBook);
  54.    hBook = 0;
  55.    book_used = 0;
  56. }
  57.  
  58. static void far *Book_alloc ( unsigned int size )
  59. {
  60.     char far * temp;
  61.     if ( book_used > 32*1024 ) return (void far *)0;
  62.     temp = xBook+book_used;
  63.     book_used += size;
  64.     return temp;
  65. }
  66.  
  67.  
  68. void
  69. GetOpenings (HWND hWnd)
  70.      
  71. /*
  72.    Read in the Opening Book file and parse the algebraic notation for a move
  73.    into an unsigned integer format indicating the from and to square. Create
  74.    a linked list of opening lines of play, with entry->next pointing to the
  75.    next line and entry->move pointing to a chunk of memory containing the
  76.    moves. More Opening lines of up to 256 half moves may be added to
  77.    gnuchess.book.
  78. */
  79. #ifndef BOOK
  80. #define BOOK "/usr/games/lib/gnuchess.book"
  81. #endif /* BOOK */     
  82. {
  83.   FILE *fd;
  84.   int c, i, j, side;
  85.  
  86.   /* char buffr[2048]; */
  87.   struct BookEntry far  *entry;
  88.   unsigned short mv, far  *mp, tmp[100];
  89.   char lpFile[_MAX_FNAME+_MAX_EXT+_MAX_DRIVE+_MAX_DIR+1];
  90.   char sFname[_MAX_FNAME], sExt[_MAX_EXT], sDrive[_MAX_DRIVE], sDir[_MAX_DIR];
  91.  
  92.   GetModuleFileName ( GetWindowWord(hWnd, GWW_HINSTANCE),
  93.                       lpFile, sizeof(lpFile) );
  94.  
  95.   _splitpath ( lpFile, sDrive, sDir, sFname, sExt);
  96.   _makepath  ( lpFile, sDrive, sDir, "gnuchess", "boo");
  97.  
  98.   fd = fopen (lpFile, "r");
  99.  
  100. /*  if ((fd = fopen (BOOK, "r")) == NULL)
  101.     fd = fopen ("gnuchess.book", "r"); */
  102.   if (fd != NULL)
  103.     {
  104.       hBook = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  105.                             (long) (32*1024 * sizeof (char)) );
  106.  
  107.       if( hBook == NULL )
  108.         {
  109.            Book = NULL;
  110.            SMessageBox (hWnd, IDS_OBAE, IDS_CHESS);
  111.            return;
  112.         }
  113.       xBook = (unsigned char far *) GlobalLock (hBook);
  114.  
  115.  
  116.       /* setvbuf(fd,buffr,_IOFBF,2048); */
  117.       Book = NULL;
  118.       i = 0;
  119.       side = white;
  120.       while ((c = parse (fd, &mv, side)) >= 0)
  121.         if (c == 1)
  122.           {
  123.             tmp[++i] = mv;
  124.             side = otherside[side];
  125.           }
  126.         else if (c == 0 && i > 0)
  127.           {
  128.             entry = (struct BookEntry far *) Book_alloc ( sizeof (struct BookEntry));
  129.             mp = (unsigned short far *) Book_alloc ( (i + 1) * sizeof (unsigned short));
  130.             if ( (entry == 0 ) || (mp == 0) )
  131.               {
  132.                 Book = NULL;
  133.                 SMessageBox (hWnd, IDS_OBAE, IDS_CHESS);
  134.                 GlobalUnlock ( hBook );
  135.                 GlobalFree ( hBook);
  136.                 return;
  137.               }
  138.             entry->mv = mp;
  139.             entry->next = Book;
  140.             Book = entry;
  141.             for (j = 1; j <= i; j++)
  142.               *(mp++) = tmp[j];
  143.             *mp = 0;
  144.             i = 0;
  145.             side = white;
  146.           }
  147.       fclose (fd);
  148.     }
  149.   else
  150.     SMessageBox (hWnd, IDS_OBNF, IDS_CHESS);
  151. }
  152.  
  153. void
  154. OpeningBook (unsigned short *hint)
  155.  
  156. /*
  157.   Go thru each of the opening lines of play and check for a match with the
  158.   current game listing. If a match occurs, generate a random number. If this
  159.   number is the largest generated so far then the next move in this line
  160.   becomes the current "candidate". After all lines are checked, the
  161.   candidate move is put at the top of the Tree[] array and will be played by
  162.   the program. Note that the program does not handle book transpositions.
  163. */
  164.  
  165. {
  166.   short j, pnt;
  167.   unsigned short m, far *mp;
  168.   unsigned r, r0;
  169.   struct BookEntry far *p;
  170.  
  171.   srand ((unsigned int) time ((long *) 0));
  172.   r0 = m = 0;
  173.   p = Book;
  174.   while (p != NULL)
  175.     {
  176.       mp = p->mv;
  177.       for (j = 1; j <= GameCnt; j++)
  178.         if (GameList[j].gmove != *(mp++))
  179.           break;
  180.       if (j > GameCnt)
  181.         if ((r = urand ()) > r0)
  182.           {
  183.             r0 = r;
  184.             m = *mp;
  185.             *hint = *(++mp);
  186.           }
  187.       p = p->next;
  188.     }
  189.  
  190.   for (pnt = TrPnt[1]; pnt < TrPnt[2]; pnt++)
  191.     if (((Tree[pnt].f << 8) | Tree[pnt].t) == m)
  192.       Tree[pnt].score = 0;
  193.   pick (TrPnt[1], TrPnt[2] - 1);
  194.   if (Tree[TrPnt[1]].score < 0) {
  195.    FreeBook ();
  196.    Book = NULL;
  197.   }
  198. }
  199.  
  200.  
  201.  
  202.  
  203.